# coding=utf-8
from decimal import Decimal

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
from django.utils import timezone

from sutras import enums, fields


class CustomUserManager(BaseUserManager):

    def create_user(self, uid, password=None):
        if not uid:
            raise ValueError("uid must be set")
        user = self.model(uid=uid)
        user.set_password(password)
        user.save(using=self._db)
        UserLevelInfo.objects.create(user=user)
        return user

    def create_superuser(self, uid, password=None):
        user = self.create_user(uid, password=password)
        user.is_admin = True
        user.save(using=self._db)
        return user


class CustomUser(AbstractBaseUser):
    uid = models.CharField(max_length=191, unique=True)
    nickname = models.CharField(max_length=191, blank=True, null=True)
    avatar_url = models.CharField(max_length=191, blank=True, null=True)
    email = models.CharField(max_length=191, blank=True, null=True)
    phone = models.CharField(max_length=191, blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    join_time = models.DateTimeField(default=timezone.now)
    type = models.SmallIntegerField(default=0)
    admin_role = models.SmallIntegerField(
        default=enums.UserAdminRole.super_admin)
    status = models.SmallIntegerField(default=0)
    open_id = models.CharField(max_length=191, blank=True, null=True)

    objects = CustomUserManager()

    USERNAME_FIELD = "uid"
    REQUIRED_FIELDS = []

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    def get_full_name(self):
        return self.uid

    def get_short_name(self):
        return self.uid

    @property
    def is_staff(self):
        return self.is_admin


class Sutra(models.Model):
    name = models.CharField(max_length=191)
    type = models.SmallIntegerField(default=1)
    description = models.CharField(max_length=800)
    cover_url = models.CharField(max_length=191)
    # status 1:normal
    status = models.SmallIntegerField(default=1)

    class Meta:
        db_table = "sutra"
        verbose_name = "佛经表"


class SutraEdition(models.Model):
    art_no = models.CharField(max_length=191, db_index=True)  # 货号
    sutra = models.ForeignKey(Sutra)
    name = models.CharField(max_length=191)
    description = models.CharField(max_length=800)
    cover_url = models.CharField(max_length=191)
    author = models.CharField(max_length=191)
    total_page_num = models.IntegerField()
    total_char_num = models.IntegerField()
    char_url = models.CharField(max_length=191, default='')
    price = fields.CurrencyField()
    status = models.SmallIntegerField(default=1)
    print_price = fields.CurrencyField()

    class Meta:
        db_table = "sutra_edition"
        verbose_name = "佛经版本表"


class SutraEditionPage(models.Model):
    sutra_edition = models.ForeignKey(SutraEdition)
    content = models.CharField(max_length=8000)
    original_cimg_url = models.CharField(max_length=191)
    hyaline_cimg_url = models.CharField(max_length=191)
    twov_cimg_url = models.CharField(max_length=191, default='')
    page_num = models.IntegerField()
    total_char_num = models.IntegerField()
    status = models.IntegerField(default=0)
    sf = models.CharField(max_length=191, default='1')
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "sutra_edition_page"
        verbose_name = "佛经版本页表"
        unique_together = ("sutra_edition", "page_num")


class SutraEditionChar(models.Model):
    page = models.ForeignKey(SutraEditionPage)
    page_num = models.IntegerField()
    index = models.IntegerField()
    text = models.CharField(max_length=1)
    img_url = models.CharField(max_length=191)
    x_positions = models.CharField(max_length=191, default='')
    y_positions = models.CharField(max_length=191, default='')

    class Meta:
        db_table = "sutra_edition_char"
        verbose_name = "佛经文字表"


class TranscriptionOrder(models.Model):
    serial_num = models.CharField(max_length=191, db_index=True, unique=True)
    user = models.ForeignKey(CustomUser)
    user_transcription = models.ForeignKey("UserTranscription")
    sutra_edition = models.ForeignKey(SutraEdition)
    total_price = fields.CurrencyField()
    total_should_pay = fields.CurrencyField()
    status = models.SmallIntegerField()
    pay_status = models.SmallIntegerField()
    remark = models.CharField(max_length=800, blank=True, null=True)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "transcription_order"
        verbose_name = "抄写订单表"


class PrintOrder(models.Model):
    serial_num = models.CharField(max_length=191, db_index=True, unique=True)
    user = models.ForeignKey(CustomUser)
    user_transcription = models.ForeignKey("UserTranscription")
    sutra_edition = models.ForeignKey(SutraEdition)
    # layout_config = models.ForeignKey("LayoutConfig")
    total_price = fields.CurrencyField()
    total_should_pay = fields.CurrencyField()
    status = models.SmallIntegerField()
    pay_method = models.SmallIntegerField(default=enums.PayMethod.wechat)
    pay_status = models.SmallIntegerField()
    remark = models.CharField(max_length=800)
    create_time = models.DateTimeField(auto_now_add=True)
    pay_time = models.DateTimeField(null=True)

    class Meta:
        db_table = "print_order"
        verbose_name = "印刷订单表"


class PrintOrderDetail(models.Model):
    print_order = models.ForeignKey("PrintOrder")
    user = models.ForeignKey(CustomUser)
    print_num = models.IntegerField(default=0)
    donate_num = models.IntegerField(default=0)
    contact = models.CharField(max_length=191, blank=True, null=True)
    phone = models.CharField(max_length=191, blank=True, null=True)
    address = models.CharField(max_length=191, blank=True, null=True)
    postcode = models.CharField(max_length=191, blank=True, null=True)
    type = models.SmallIntegerField()
    temple = models.ForeignKey("Temple", null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True)
    print_time = models.DateTimeField(null=True)

    class Meta:
        db_table = "print_order_detail"
        verbose_name = "印刷订单详情表"


class UserTranscription(models.Model):
    sutra_edition = models.ForeignKey(SutraEdition)
    user = models.ForeignKey(CustomUser)
    user_layout = models.ForeignKey("UserLayout")
    vow_text = models.CharField(max_length=191, blank=True, null=True)
    status = models.SmallIntegerField()
    signature_img_url = models.CharField(max_length=191, blank=True, null=True)
    signature_text = models.CharField(max_length=191, blank=True, null=True)
    finish_char_num = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    vow_time = models.DateTimeField(null=True)
    finish_time = models.DateTimeField(null=True)

    class Meta:
        db_table = "user_transcription"
        verbose_name = "用户抄写表"


class UserPageTranscription(models.Model):
    user_transcription = models.ForeignKey(UserTranscription)
    page_num = models.IntegerField()
    assistant_user = models.ForeignKey(CustomUser, null=True)
    finish_char_num = models.IntegerField(default=0)
    total_char_num = models.IntegerField()
    status = models.SmallIntegerField(
        default=enums.UserPageTranscription.not_finished)
    create_time = models.DateTimeField(auto_now_add=True)
    finish_time = models.DateTimeField(null=True)

    class Meta:
        db_table = "user_page_transcription"
        verbose_name = "用户抄写页表"
        unique_together = ("user_transcription", "page_num")


class UserAddress(models.Model):
    user = models.ForeignKey(CustomUser)
    name = models.CharField(max_length=191, blank=True, null=True)
    contact = models.CharField(max_length=191)
    phone = models.CharField(max_length=191)
    address = models.CharField(max_length=191)
    postcode = models.CharField(max_length=191, blank=True, null=True)
    type = models.SmallIntegerField(default=enums.UserAddressType.default)
    status = models.SmallIntegerField(default=enums.UserAddressStatus.normal)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "user_address"
        verbose_name = "用户地址表"


class AssistantRelation(models.Model):
    sutra_edition = models.ForeignKey(SutraEdition)
    user_transcription = models.ForeignKey(UserTranscription)
    owner_user = models.ForeignKey(CustomUser, related_name="owner_user")
    assistant_user = models.ForeignKey(
        CustomUser, related_name="assistant_user")
    accept_time = models.DateTimeField(auto_now_add=True)
    page_num = models.IntegerField()
    status = models.SmallIntegerField(
        enums.AssistantRelationStatus.normal)

    class Meta:
        db_table = "assistant_relation"
        verbose_name = "同修关系表"


class UserLevelInfo(models.Model):
    user = models.OneToOneField(CustomUser, unique=True)
    total_time = models.BigIntegerField(default=0)
    finish_page_num = models.IntegerField(default=0)
    finish_char_num = models.IntegerField(default=0)
    finish_book_num = models.IntegerField(default=0)
    print_book_num = models.IntegerField(default=0)
    assist_page_num = models.IntegerField(default=0)
    total_cost = fields.CurrencyField(default=Decimal())

    class Meta:
        db_table = "user_level_info"
        verbose_name = "用户修行果位表"


class UserFavorite(models.Model):
    user = models.ForeignKey(CustomUser)
    sutra_edition = models.ForeignKey(SutraEdition)
    status = models.SmallIntegerField(default=enums.UserFavoriteStatus.normal)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "user_favorite"
        verbose_name = "用户收藏表"
        unique_together = ("user", "sutra_edition")


class VowConfig(models.Model):
    name = models.CharField(max_length=191, blank=True, null=True)
    content = models.CharField(max_length=191)
    status = models.SmallIntegerField(default=0)

    class Meta:
        db_table = "vow_config"
        verbose_name = "发愿文配置表"


class UserLayout(models.Model):
    user = models.ForeignKey(CustomUser)
    typeset = models.ForeignKey("TypesetConfig")
    material = models.ForeignKey("MaterialConfig")
    bind = models.ForeignKey("BindConfig")
    font_color = models.ForeignKey("FontColorConfig")

    class Meta:
        db_table = "user_layout"
        verbose_name = "用户格式表"


class LayoutConfig(models.Model):
    name = models.CharField(max_length=191)
    xuan_img_url = models.CharField(max_length=191)
    font_color = models.CharField(max_length=191)
    bind_type = models.IntegerField()
    status = models.SmallIntegerField()
    price = fields.CurrencyField()

    class Meta:
        db_table = "layout_config"
        verbose_name = "抄写格式配置表"


class Temple(models.Model):
    name = models.CharField(max_length=191, unique=True)
    img_url = models.CharField(max_length=191)
    status = models.SmallIntegerField(default=enums.TempleStatus.normal)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "temple"
        verbose_name = "施愿处表"


class HotwordConfig(models.Model):
    content = models.CharField(max_length=191)
    index = models.IntegerField(unique=True)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "hotword_config"
        verbose_name = "热词配置表"


class FontColorConfig(models.Model):
    name = models.CharField(max_length=191)
    img_url = models.CharField(max_length=191)
    content = models.CharField(max_length=191)
    price = fields.CurrencyField()

    class Meta:
        db_table = "font_color_config"
        verbose_name = "字体颜色配置表"


class TypesetConfig(models.Model):
    name = models.CharField(max_length=191)
    img_url = models.CharField(max_length=191)
    content = models.CharField(max_length=191)
    price = fields.CurrencyField()
    status = models.IntegerField(default=1)

    class Meta:
        db_table = "typeset_config"
        verbose_name = "排版配置表"


class MaterialConfig(models.Model):
    name = models.CharField(max_length=191)
    img_url = models.CharField(max_length=191)
    content = models.CharField(max_length=191)
    price = fields.CurrencyField()

    class Meta:
        db_table = "material_config"
        verbose_name = "纸张材质配置表"


class BindConfig(models.Model):
    name = models.CharField(max_length=191)
    img_url = models.CharField(max_length=191)
    content = models.CharField(max_length=191)
    price = fields.CurrencyField()

    class Meta:
        db_table = "bind_config"
        verbose_name = "装订方式配置表"
